import json
import random
import time

import execjs
import pymongo
import requests
from lxml import etree


class OuYeel:
    def __init__(self):
        self.mongo_client = pymongo.MongoClient(host='127.0.0.1', port=27017)
        self.collection = self.mongo_client['py_spider']['ou_yeel_info']
        self.func_code = None
        self.content_code = None
        self.ts_code = None
        self.headers = {
            "Accept": "*/*",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "Origin": "https://www.ouyeel.com",
            "Pragma": "no-cache",
            "Referer": "https://www.ouyeel.com/search-ng/queryResource/index",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-origin",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
            "X-Requested-With": "XMLHttpRequest",
        }

        self.url = "https://www.ouyeel.com/search-ng/queryResource/index"
        # 访问首页获取cookie
        self.init()

    def init(self):
        response = session.get(self.url, headers=self.headers)
        print("第一次响应码=>", response.status_code)
        print("第一次cookie值：", session.cookies)
        obj = etree.HTML(response.text)  # type: ignore
        self.ts_code = obj.xpath('//script[1]/text()')[0]
        self.content_code = obj.xpath('//meta[2]/@content')[0]
        func_src = 'https://www.ouyeel.com' + obj.xpath('//script[2]/@src')[0]
        self.func_code = requests.get(func_src, headers=self.headers).text

    def refresh_cookie(self):
        with open('main.js', encoding='utf-8') as f:
            code = f.read().replace('func_code', self.func_code) \
                .replace('content_code', self.content_code).replace('ts_code', self.ts_code)
        res = execjs.compile(code).call('get_cookie')
        res = res.split(';')[0].split('=')
        print(res)
        return res

    def get_data(self, page):
        # 每次刷新cookie值
        res = self.refresh_cookie()
        session.cookies[res[0]] = res[1]

        url = "https://www.ouyeel.com/search-ng/commoditySearch/queryCommodityResult"
        # headers['X-Tingyun-Id'] = "shNg2wpepqo" + ";r=" + str(int(time.time() * 1000) % 1000000000)
        data = {
            "pageIndex": page,
            "pageSize": 50,
            "industryComponent": "",
            "channel": "",
            "productType": "",
            "sort": "",
            "warehouseCode": "",
            "key_search": "",
            "is_central": "",
            "searchField": "",
            "companyCode": "",
            "inquiryCategory": "",
            "inquirySpec": "",
            "provider": "",
            "shopCode": "",
            "steelFactory": "",
            "resourceIds": "",
            "jsonParam": {},
            "excludeShowSoldOut": ""
        }
        response = session.post(url, headers=self.headers, data={"criteriaJson": json.dumps(data)})

        print(f"获取第{page}页数据：", response.status_code)
        result = response.json()
        for res in json.loads(result['resultList']):
            item = dict()
            if 'actualEntryMode' in res:
                item['title'] = " ".join(
                    [res['manufactureName'], res['productName'] if "productName" in res else "",
                     res['qualityGradeName'], res['spec'],
                     res['shopSign']])
                item['price'] = res['publishPrice']
                item['address'] = res['warehouseName']

            else:
                item['title'] = " ".join(
                    [res['manufactureName'], res['productName'], res['resourceObj']['qualityGradeName'],
                     res['resourceObj']['spec'], res['shopSign']])
                item['price'] = res['resourceObj']['publishPrice']
                item['address'] = res['resourceObj']['warehouseName']

            self.collection.insert_one(item)
            print("入库成功：", item)

    def __del__(self):
        self.mongo_client.close()


if __name__ == '__main__':
    session = requests.session()
    o = OuYeel()
    for i in range(1, 11):
        o.get_data(i)
        time.sleep(random.randint(1, 3))
